package cn.itcast.ssm.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
public class MyBatisUtil {

 private static final Logger LOG = Logger.getLogger(MyBatisUtil.class);
 private static SqlSessionFactory factory;
 static{
     SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
     try { InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
         factory = builder.build(inputStream); }
         catch (IOException e) {
         LOG.info("com.guigu.util.MyBatisUtil static{},msg 加载配置文件出错了"+e.getMessage());
     } }

     public static SqlSession getSqlSession(){
     if(factory == null){ throw new RuntimeException("factory is  null");
     } return factory.openSession(); }

}
